package com.zzl.finance.data.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.finance.data.service.CodeService;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.constant.ParameterCalibrationConstants;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Pattern;

/**
 * 验证码
 *
 * @author gull
 */
@ApidocController("手机验证码")
@RestController
@RequestMapping("data/code")
@Slf4j
public class CodeController extends WebBaseCtrl {

    @Autowired
    @SuppressWarnings("all")
    private CodeService codeService;


    @GetMapping("/allowAuth/register/{phone}/{type}")
    @ApidocOperation(description = "发送注册验证码", operationId = "data/code/allowAuth/login/phone/type/e2")
    @ApidocReturnR
    public Result getLoginCode(@ApidocParam(name = "phone", description = "手机号") @PathVariable("phone") String phone
            , @ApidocParam(name = "type", description = "验证码类型，HANDLER：经办人手机号，LEGAL：法人手机号") @PathVariable("type") String type) {
        try {
            if (!Pattern.compile(ParameterCalibrationConstants.REGULAR_EXPRESSION_PHONE_NUMBER_VALIDITY).matcher(phone).matches()) {
                return Result.error("请输入正确手机号！");
            }
            codeService.getLoginCode(phone, type);
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }


    @GetMapping("/allowAuth/voucherRegister/{phone}/{type}")
    @ApidocOperation(description = "电子债券发送注册验证码", operationId = "data/code/allowAuth/login/phone/type/e3")
    @ApidocReturnR
    public Result getVoucherLoginCode(@ApidocParam(name = "phone", description = "手机号") @PathVariable("phone") String phone
            , @ApidocParam(name = "type", description = "验证码类型，HANDLER：经办人手机号，LEGAL：法人手机号") @PathVariable("type") String type) {
        if (!Pattern.compile(ParameterCalibrationConstants.REGULAR_EXPRESSION_PHONE_NUMBER_VALIDITY).matcher(phone).matches()) {
            return Result.error("请输入正确手机号！");
        }
        return codeService.getVoucherLoginCode(phone);

    }

    @GetMapping("/allowAuth/verificationVoucherLoginCode/{phone}/{code}")
    @ApidocOperation(description = "验证电子债券验证码", operationId = "data/code/allowAuth/login/phone/type/e4")
    @ApidocReturnR
    public Result verificationVoucherLoginCode(@ApidocParam(name = "code", description = "验证码") @PathVariable("code") String code,
                                               @ApidocParam(name = "phone", description = "手机号") @PathVariable("phone") String phone) throws SysException {
        return codeService.verificationVoucherLoginCode(code, phone);
    }
}
